#! /usr/bin/env python  
# -*- coding: utf-8 -*-  
#@author yuqingxu@ctrip.com  
#@version 2015-09-06  09:00

import logging
import logging.config

class logUtil:
    loggerName=""
    logger = logging.getLogger(loggerName)

    '级别：NOTSET < DEBUG < INFO < WARNING < ERROR < CRITICA'
    def initLog(self) :   
        # print logging.__file__     
        logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S',
                    filename='~/temp/base.log',
                    filemode='a')
        # 定义一个Handler打印INFO及以上级别的日志到sys.stderr
        console = logging.StreamHandler()
        console.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
        console.setLevel(logging.NOTSET)        
        logging.getLogger('').addHandler(console)

        #定义一个RotatingFileHandler，最多备份5个日志文件，每个日志文件最大10M
        Rthandler = RotatingFileHandler('~/temp/all.log', maxBytes=50*1024*1024,backupCount=5)
        Rthandler.setLevel(logging.NOTSET)
        formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
        Rthandler.setFormatter(formatter)
        logging.getLogger('').addHandler(Rthandler)

    def loadConfig(self,fileNameWithPath):
        logging.config.fileConfig(fileNameWithPath)

    def setLogger(self,loggerName) :        
        self.logger = logging.getLogger(loggerName)               

    def info(self,msg):
        self.logger.info(msg)    

    # @staticmethod
    def debug(self,msg):
        # print 'sdfsdf'
        self.logger.debug(msg)

    def warn(self,msg):
        self.logger.warning(msg)

    def error(self,msg):
        self.logger.error(msg)

    def __init__(self):                   
        self.initLog()
        self.logger = logging.getLogger("")    

    def __init__(self,configFile):
        global logger
        # print "logging init..."
        self.loadConfig(configFile)
        # print "config has load."
        self.logger = logging.getLogger("")        
        # print "logging init end."

